﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChungNhanSinhVien.Entities;
using ChungNhanSinhVien.Infrastructures;
using ChungNhanSinhVien.Entities.SinhVien;

namespace ChungNhanSinhVien.Models
{
    public class StudentRepository
    {
        Web_ChungNhanSinhVienEntities data;
        
        public StudentRepository()
        {
            //if (data == null)
                data = new Web_ChungNhanSinhVienEntities();
        }

        public Student GetStudentByID(string studentID)
        {
            var kq = (from m in data.Students
                      where m.StudentID.Equals(studentID)
                      select m).FirstOrDefault();
            return kq;
        }

        public bool KiemTraTaiKhoan(string studentID, string matkhau, out SinhVien kq)
        {            
            string md5mk = new SharedFunc().CreateMD5Hash(matkhau);
            //kq = (from m in data.Students
            //      where m.StudentID.Equals(studentID)
            //      && m.MatKhau.Equals(md5mk)
            //      select m).FirstOrDefault();
            var mkdata = new Web_SinhVienEntities();
            kq = (from m in mkdata.SinhViens
                  where m.MSSV.Equals(studentID)
                  && m.MatKhau.Equals(md5mk)
                  select m).FirstOrDefault();
            if (kq != null)
                return true;
            return false;
        }

        public bool KiemTraTaiKhoanDaTonTaiTheoMaSV(string masv)
        {
            var kq = (from m in data.Students
                      where m.StudentID.Equals(masv)
                      select m.StudentID).FirstOrDefault();
            if (kq != null)
                return true;
            return false;
        }
       
    }
}